package com.key.web_admin.controller;

import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.key.common.base.RespResult;
import com.key.common.utils.OddNumbersUtils;
import com.key.modules.dto.AddCarrierStDTO;
import com.key.modules.dto.CarrierPayDTO;
import com.key.modules.dto.CarrierStDTO;
import com.key.modules.entity.CarrierSt;
import com.key.modules.service.CarrierPayService;
import com.key.modules.vo.CarrierPayVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/carrierPayable")

public class CarrierPayController {

    @Autowired
    CarrierPayService carrierPayService;

    /**
     * 查询信息
     * @return
     */
    @PostMapping("/findCarrierPayable")
    public RespResult findCarrierPayable(@RequestBody CarrierPayDTO carrierPayDTO){
        Map<String, Object> resultMap=carrierPayService.findCarrierPayable(carrierPayDTO);
        RespResult respResult =RespResult.success(Integer.parseInt(String.valueOf(resultMap.get("total") )),resultMap.get("data"));
        return respResult;
    }

    /**
     * 创建对账单
     * @return
     */
    @PostMapping("/creatCarrierStatement")
    public RespResult creatCarrierStatement(@RequestBody AddCarrierStDTO addCarrierStDTO){
        CarrierSt carrierSt=new CarrierSt();
        carrierSt.setStatementNumber(addCarrierStDTO.getTransportNo());
        carrierSt.setTeaMoney(addCarrierStDTO.getOrderMoney());
        carrierSt.setBillingTime(addCarrierStDTO.getDepartTime());
        carrierSt.setCreationTime(OddNumbersUtils.getDay());
        carrierSt.setCreator("王五");
        carrierSt.setPaymentObject("暂无");
        carrierSt.setBillingTime("2011-12-12");
        carrierSt.setBillingStatus(0);
        carrierSt.setInvoiceStatus(0);
        int i=carrierPayService.creatCarrierStatement(carrierSt);
        return i==0?RespResult.error("创建失败！"):RespResult.success();
    }

    /**
     * 导出excel
     * @return
     */
    @GetMapping("/exportData")

    public RespResult exportData(){
        //excel文件名
        String fileName = "D:\\\\承运商应付"+System.currentTimeMillis()+".xls";
        CarrierPayDTO carrierPayDTO=new CarrierPayDTO();
        carrierPayDTO.setCurrentPage(0);
        carrierPayDTO.setPageSize(1000);
        RespResult respResult=findCarrierPayable(carrierPayDTO);
        EasyExcel.write(fileName, CarrierPayVO.class).sheet("承运商应付").doWrite((ArrayList)respResult.getData());
        return RespResult.success("保存成功路径："+fileName);
    }

}
